<?php

class Service extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function all() {
		$cond = array('status' => 1);
		$this->db->where($cond);
		$query = $this->db->get('service');
		return $query->result_array();
	}

	function map() {
		$data = $this->all();
		$r = array();
		foreach ($data as $v) {
			$r[$v['id']] = $v['service'];
		}
		return $r;
	}
	
	function add($cond) {
		if ($this->exist($cond['service'])) return FALSE;
		$cond['status'] = 1;
		$cond['created_at'] = date('Y-m-d h:i:s', mktime());
		$cond['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->insert('service', $cond);
		return $this->db->insert_id();
	}

	function exist($service) {
		$cond = array('service' => $service, 'status' => 1);
		$this->db->where($cond);
		$query = $this->db->get('service');
		if ($query->num_rows() >= 1) return TRUE;
		return FALSE;	
	}
	function filter($data) {
		$msg = array();
		if (!array_key_exists('service', $data)) $msg[] = '数据错误'; 
		if (trim($data['service']) == '') $msg[] = '数据为空';
		$status = FALSE;
		if (empty($msg)) {
			$status = TRUE;
		}
		return array('status' => $status, 'msg' => $msg);
	}
	function del($id) {
		$data = array('status' => 0);
		$data['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->where('id', $id);
		$this->db->update('service', $data); 
	}

	function find($id) {
		$this->db->where('id', $id); 
		$query = $this->db->get('service');
		return $query->result_array();
	}
}
